//
//  DevSettingsController.swift
//  ToBeBigFE
//
//  Created by mingo on 2020/2/24.
//  Copyright © 2020 com.example. All rights reserved.
//

import Foundation
import UIKit
import SwiftyFORM

class DevSettingsController : FormViewController {
    
    lazy var settings: UserDefaults = { return WebManager.shared.settings }()
    
    lazy var enabledSwitch: SwitchFormItem = {
        let instance = SwitchFormItem()
        instance.title = "开启"
        instance.setValue(settings.bool(forKey: "enabled"), animated: false)
        instance.switchDidChangeBlock = {
            self.settings.setValue($0, forKey: "enabled")
        }
        return instance
    }()
    
    lazy var projectTextField: TextFieldFormItem = {
        let instance = TextFieldFormItem()
        instance.title = "项目"
        instance.placeholder = "未设置"
        
        instance.textAlignment = .right
        instance.value = self.settings.string(forKey: "project") ?? ""
        instance.textDidChangeBlock = {
            self.settings.setValue($0, forKey: "project")
        }
        return instance
    }()
    
    lazy var serverTextField: TextFieldFormItem = {
        let instance = TextFieldFormItem()
        instance.title = "服务器"
        instance.placeholder = "未设置"
        instance.textAlignment = .right
        instance.value = self.settings.string(forKey: "server") ?? ""
        instance.textDidChangeBlock = {
            self.settings.setValue($0, forKey: "server")
        }
        return instance
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "开发环境"
        let leftCloseBtn = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(onClickClose))
        navigationItem.leftBarButtonItem = leftCloseBtn
    }
    
    @objc private func onClickClose() {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func populate(_ builder: FormBuilder) {
        builder += SectionHeaderTitleFormItem().title("Web开发环境")
        builder += enabledSwitch
        builder += projectTextField
        builder += serverTextField
    }
    
}
